package utils;

import entity.ProjectEntity;
import entity.TableEntity;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * describe 模板工具类
 * author wencong 2022/3/3
 */
public class VmUtils {

    /** 渲染模板 */
    public static String render(Map<String, Object> map, String vmPath) {
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        VelocityContext context = new VelocityContext(map);
        StringWriter stringWriter = new StringWriter();
        Template template = Velocity.getTemplate(vmPath, "UTF-8");
        template.merge(context, stringWriter);
        return stringWriter.toString();
    }

    // 替换字符集合
    public static Map<String, Object> getReplaceMap(ProjectEntity projectEntity, TableEntity tableEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("tableEntity", tableEntity); // 表属性
        map.put("projectEntity", projectEntity); // 项目属性
        return map;
    }
}
